/*
 *  $Id: GameLog.cs 1011 2010-05-09 21:35:13Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;

using AgateLib;
using AgateLib.Geometry;

namespace Ragima {

    public class GameLog: Log {
        private int _pos;
        private bool _paused, _scrolled;
        private IHelpProvider _help;

        public GameLog(): base(Constants.Log, Data.Fonts.Log) {
            _textRightIndent = Constants.LogTextRightIndent;
            _pos = 0;
        }

        public int Position { get { return _pos; } }
        public int MaxPosition { get { return Math.Max(_data.Count - Constants.LogRows, 0) * Constants.LogRowHeight; } }
        public int ScrollPosition {
            get {
                if (MaxPosition == 0) return Constants.LogScrollHeight;
                return Constants.LogScrollHeight - (Constants.LogScrollHeight * Position / MaxPosition);
            }
        }

        public IHelpProvider Help { get { return _help; } set { _help = value; } }

        public override void Draw() {
            if (Help != null) {
                if (Help.DrawHelp(Rect.X, Rect.Y)) return;
            }
            DrawScroll();
            base.Draw();
        }

        public override Hint GetHint() {
            if (Help != null || _scrolled) return null;
            return base.GetHint();
        }

        protected override int GetStartLine() {
            return _pos / LineHeight;
        }
        protected override int GetEndLine() {
            int end = _pos / LineHeight + Constants.LogRows;
            if ((_pos % LineHeight) == 0) end--;
            return Math.Min(_data.Count -1, end);
        }
        protected override Point GetStartPoint() {
            return new Point(Rect.X, Rect.Y - _pos % LineHeight);
        }

        public void DrawScroll() {
            int pos = ScrollPosition;
            Rectangle src, dest;
            src = new Rectangle(0, 0, 20, pos);
            dest = new Rectangle(Constants.LogScrollX, Constants.LogScrollY, 20, pos);
            Data.ScrollEmpty.Draw(src, dest);
            src = new Rectangle(0, pos, 20, Constants.LogHeight - pos);
            dest = new Rectangle(Constants.LogScrollX, Constants.LogScrollY + pos, 20, Constants.LogHeight - pos);
            Data.ScrollFull.Draw(src, dest);
            Data.ScrollPointer.Draw(Constants.LogScrollX, Constants.LogScrollY + pos - 6);
        }

        public override void OnMouseLeave() {
            base.OnMouseLeave();
            _paused = false;
        }

        public override void OnClick() {
            _paused = !_paused;
        }

        public override void OnProcess() {
            bool scrolled = _scrolled;
            if (_paused) {
                _scrolled = false;
            } else {
                int prevPos = _pos;
                if (Focus) {
                    int d = MousePosition.Y - Constants.LogHeight / 2;
                    if (d < 0) {
                        _pos += (d * 10 / Constants.LogHeight - 1);
                        if (_pos < 0) _pos = 0;
                    } else {
                        _pos += (d * 10 / Constants.LogHeight + 1);
                        if (_pos > MaxPosition) _pos = MaxPosition;
                    }
                } else {
                    _pos += 10;
                    if (_pos > MaxPosition) _pos = MaxPosition;
                }
                _scrolled = (prevPos != _pos);
            }
            if (_scrolled != scrolled)
                Application.CurrentScene.UpdateHint();
        }

    }

}